/* 
 * File: Animation
 * Created: 15-JUN-2011
 * Author: Colin Gall-McDaid
 * 
 * Description: Animation class from storing a set of animation frames. Sequence
 * can be looping or non-looping.
 */
package com.gallsoft.games.framework.gamedev2d;

import com.gallsoft.games.framework.gl.TextureRegion;


public class Animation {

    public static final int ANIMATION_LOOPING = 0;
    public static final int ANIMATION_NONLOOPING = 1;
    
    private final TextureRegion[] keyFrames;
    private final float frameDuration;
    private TextureRegion currentFrame;
    
    /**
     * Constructor.
     * 
     * @param frameDuration
     *          time between each frame
     * @param keyFrames
     *          texture frame sequence
     */
    public Animation(float frameDuration, TextureRegion ... keyFrames) {
        this.frameDuration = frameDuration;
        this.keyFrames = keyFrames;
    }
    
    /**
     * Get the next animation frame
     * 
     * @param stateTime
     *          time since last frame
     * @param mode
     *          sequence mode (looping or non-looping)
     * @return
     *          next key frame in sequence
     */
    public TextureRegion getKeyFrame(float stateTime, int mode) {
        int frameNumber = (int)(stateTime / frameDuration);
        
        if (mode == ANIMATION_NONLOOPING)
            frameNumber = Math.min(keyFrames.length - 1, frameNumber);
        else
            frameNumber = frameNumber % keyFrames.length;
        
        currentFrame = keyFrames[frameNumber];
        return currentFrame;
    }
    
    public TextureRegion getKeyFrame(int frameNumber) {
        return keyFrames[frameNumber];        
    }
}
